home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / tests / tiomisc.cc < prev    next >
C/C++ Source or Header  |  1994-10-13  |  3KB  |  147 lines

  1. /* Random regression tests etc. */
  2.  
  3. #include <fstream.h>
  4. #include <stdio.h>
  5. #include <strstream.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8.  
  9. #define BUF_SIZE 4096
  10.  
  11. void
  12. test1 ()
  13. {
  14.    fstream f;
  15.    char    buf[BUF_SIZE];
  16.  
  17.    f.setbuf( buf, BUF_SIZE );
  18. }
  19.  
  20. void
  21. test2 ( )
  22. {
  23.    char string[BUF_SIZE];
  24.    ostrstream s( string, BUF_SIZE );
  25.  
  26.    s << "Bla bla bla " << 55 << ' ' << 3.23 << '\0' << endl;
  27.    cout << "Test2: " << string << endl;
  28. }
  29.  
  30.  
  31. /* Test case from Joe Buck <jbuck@Synopsys.COM>. */
  32.  
  33. class special_ofstream : public ofstream {
  34. public:
  35.     special_ofstream() : ofstream() {}
  36.     special_ofstream(int fd) : ofstream(fd) {}
  37.     special_ofstream(const char *name, int mode=ios::out, int prot=0664) {
  38.         open(name,mode,prot);
  39.     }
  40.     void open(const char *name, int mode=ios::out, int prot=0664);
  41. };
  42.  
  43. void special_ofstream::open(const char* name, int mode, int prot) {
  44.     if (strcmp(name, "<cout>") == 0) {
  45.         rdbuf()->attach(1);
  46.     }
  47.     else if (strcmp(name, "<cerr>") == 0) {
  48.         rdbuf()->attach(2);
  49.         setf(unitbuf);
  50.     }
  51.     else ofstream::open(name,mode,prot);
  52. }
  53.  
  54. void
  55. test3 ()
  56. {
  57.     {
  58.         special_ofstream o("<cout>");
  59.         o << "Hello\n";
  60.         // o is destructed now.  This should not close cout
  61.     }
  62.     {
  63.         special_ofstream o("<cout>");
  64.         o << "Line 2\n";
  65.     }
  66. }
  67.  
  68. void
  69. getline_test1 ()
  70. {
  71.   char buf[1000];
  72.   char data[] = "#include <iostream.h>\n#include <fstream.h>\n";
  73.   istrstream infile(data, strlen(data));
  74.   infile.getline(buf,1000);
  75.   infile.getline(buf,1000);
  76.  
  77.   cout << buf << '\n';
  78. }
  79.  
  80. getline_test2 ()
  81. {
  82.   char data[] = "Line one.\nline 2.\n";
  83.   char line[100];
  84.   istrstream strin(data, strlen(data));
  85.   strin.getline(line, 10);
  86.   cout << "line: " << line << ", count: " << strin.gcount () << "\n";
  87. }
  88.  
  89. class A : private ostream
  90. {
  91. public:
  92.   A(streambuf* s);
  93.   ostream::flush;
  94. };
  95. A::A(streambuf* s)
  96. : ostream(s)
  97. {
  98. }
  99.  
  100. flush1_test()
  101. {
  102.   A os(cout.rdbuf());
  103.   os.flush();
  104. }
  105.  
  106. void
  107. reread_test ()
  108. {  // This is PR 5486.
  109.    int tag_char;
  110.    char *fname = "Makefile";
  111.    int mode = O_RDONLY;
  112.    filebuf file_p; 
  113.  
  114.    int fd = ::open(fname, mode, 0666);
  115.    file_p.attach(fd); 
  116.  
  117.    istream d_istream(&file_p);
  118.  
  119.    // Read a character from the stream, save it and put it back.
  120.    tag_char = d_istream.get();
  121.    int save_char = tag_char;
  122.    d_istream.putback((char) tag_char);
  123.  
  124.    // Uncomment then next statement and the next get will be EOF.
  125.    streampos pos = d_istream.tellg();  
  126.  
  127.    // Re-read the first character
  128.    tag_char = d_istream.get();
  129.  
  130.    cout << "reread_test: " << (char)save_char << " " << (char)tag_char << "\n";
  131.    cout.flush();
  132.    exit (0);
  133.  
  134. }
  135.  
  136. int main( )
  137. {
  138.   test1 ();
  139.   test2 ();
  140.   test3 ();
  141.   getline_test1 ();
  142.   getline_test2 ();
  143.   flush1_test ();
  144.   reread_test ();
  145.   return 0;
  146. }
  147.